<template>
  <view class="pending-feedbacks-container">
    <!-- 头部 -->
    <view class="header">
      <view class="header-left" @click="goBack">
        <text class="back-icon">←</text>
      </view>
      <text class="title">未处理反馈</text>
      <view class="header-right">
        <text class="count">{{ pendingFeedbacks.length }}</text>
      </view>
    </view>

    <!-- 加载状态 -->
    <view v-if="loading" class="loading">
      <text>加载中...</text>
    </view>

    <!-- 空状态 -->
    <view v-else-if="pendingFeedbacks.length === 0" class="empty">
      <text class="empty-icon">📝</text>
      <text class="empty-text">暂无未处理的反馈</text>
      <button class="add-btn" @click="goToAddFeedback">添加反馈</button>
    </view>

    <!-- 反馈列表 -->
    <view v-else class="feedback-list">
      <view 
        v-for="feedback in pendingFeedbacks" 
        :key="feedback.id" 
        class="feedback-item"
        @click="goToFeedbackDetail(feedback.id)"
      >
        <view class="feedback-header">
          <text class="feedback-content">{{ feedback.content }}</text>
          <view class="status-badge status-pending">待处理</view>
        </view>
        
        <view class="feedback-meta">
          <text class="feedback-time">{{ formatTime(feedback.createdAt) }}</text>
          <text v-if="feedback.adminRealName" class="feedback-admin">
            反馈给：{{ feedback.adminRealName }}
          </text>
        </view>
        
        <view v-if="feedback.images" class="feedback-images">
          <view 
            v-for="(image, index) in getImageList(feedback.images)" 
            :key="index" 
            class="image-thumb"
          >
            <image :src="image" class="thumb-image" mode="aspectFill" />
          </view>
        </view>
      </view>
    </view>

    <!-- 底部操作 -->
    <view class="bottom-actions">
      <button class="action-btn" @click="goToAddFeedback">
        <text class="btn-icon">+</text>
        <text class="btn-text">添加反馈</text>
      </button>
    </view>
  </view>
</template>

<script>
import { ref, onMounted } from 'vue'
import request from '@/utils/request.js'
import tokenManager from '@/utils/token.js'

export default {
  name: 'PendingFeedbacks',
  setup() {
    const loading = ref(false)
    const pendingFeedbacks = ref([])

    // 获取未处理反馈列表
    const fetchPendingFeedbacks = async () => {
      if (!tokenManager.isLoggedIn()) {
        uni.showModal({
          title: '提示',
          content: '请先登录后再查看反馈',
          showCancel: false,
          confirmText: '去登录',
          success: (res) => {
            if (res.confirm) {
              uni.navigateTo({
                url: '/pages/login/login'
              })
            }
          }
        })
        return
      }

      loading.value = true
      try {
        const response = await request.request({
          url: '/feedback/pending',
          method: 'GET'
        })

        if (response.code === 200) {
          pendingFeedbacks.value = response.data || []
        } else {
          console.error('获取未处理反馈失败:', response)
          uni.showToast({
            title: response.message || '获取失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('获取未处理反馈错误:', error)
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        })
      } finally {
        loading.value = false
      }
    }

    // 获取图片列表
    const getImageList = (images) => {
      if (!images) return []
      return images.split(',').filter(img => img.trim()).slice(0, 3)
    }

    // 格式化时间
    const formatTime = (dateString) => {
      if (!dateString) return ''
      const date = new Date(dateString)
      const now = new Date()
      const diff = now - date
      
      if (diff < 60000) { // 1分钟内
        return '刚刚'
      } else if (diff < 3600000) { // 1小时内
        return Math.floor(diff / 60000) + '分钟前'
      } else if (diff < 86400000) { // 1天内
        return Math.floor(diff / 3600000) + '小时前'
      } else {
        return `${date.getMonth() + 1}-${date.getDate()}`
      }
    }

    // 跳转到反馈详情页面
    const goToFeedbackDetail = (feedbackId) => {
      uni.navigateTo({
        url: `/pages/feedback/feedback-detail?id=${feedbackId}`
      })
    }

    // 跳转到添加反馈页面
    const goToAddFeedback = () => {
      uni.navigateTo({
        url: '/pages/feedback/add-feedback'
      })
    }

    // 返回上一页
    const goBack = () => {
      uni.navigateBack()
    }

    onMounted(() => {
      fetchPendingFeedbacks()
    })

    return {
      loading,
      pendingFeedbacks,
      fetchPendingFeedbacks,
      getImageList,
      formatTime,
      goToFeedbackDetail,
      goToAddFeedback,
      goBack
    }
  }
}
</script>

<style scoped>
.pending-feedbacks-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 30rpx;
  background-color: white;
  border-bottom: 2rpx solid #f0f0f0;
}

.header-left, .header-right {
  width: 80rpx;
  display: flex;
  justify-content: center;
}

.back-icon {
  font-size: 36rpx;
  color: #333;
  font-weight: bold;
}

.title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.count {
  font-size: 24rpx;
  color: #ff4757;
  background-color: #ffe6e6;
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
}

.loading, .empty {
  text-align: center;
  padding: 100rpx 30rpx;
}

.empty-icon {
  font-size: 80rpx;
  display: block;
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
  display: block;
  margin-bottom: 30rpx;
}

.add-btn {
  background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
  color: white;
  border: none;
  border-radius: 12rpx;
  padding: 20rpx 40rpx;
  font-size: 26rpx;
}

.feedback-list {
  padding: 20rpx;
}

.feedback-item {
  background-color: white;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.feedback-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16rpx;
}

.feedback-content {
  flex: 1;
  font-size: 26rpx;
  color: #333;
  line-height: 1.4;
  margin-right: 12rpx;
}

.status-badge {
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  font-size: 20rpx;
  white-space: nowrap;
}

.status-pending {
  background-color: #fff3cd;
  color: #856404;
}

.feedback-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16rpx;
}

.feedback-time {
  font-size: 22rpx;
  color: #999;
}

.feedback-admin {
  font-size: 22rpx;
  color: #666;
}

.feedback-images {
  display: flex;
  gap: 12rpx;
}

.image-thumb {
  width: 80rpx;
  height: 80rpx;
  border-radius: 6rpx;
  overflow: hidden;
}

.thumb-image {
  width: 100%;
  height: 100%;
}

.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20rpx 30rpx;
  background-color: white;
  border-top: 2rpx solid #f0f0f0;
}

.action-btn {
  width: 100%;
  height: 80rpx;
  background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
  border-radius: 12rpx;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-icon {
  font-size: 32rpx;
  color: white;
  margin-right: 8rpx;
}

.btn-text {
  font-size: 26rpx;
  color: white;
  font-weight: bold;
}
</style>


